<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\UserTable;
use App\Admin\Repositories\UserStorePlan;
use App\Models\FirmProfession;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class UserStorePlanController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new UserStorePlan(['profession:id,name','user:id,avatar,nickname']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('user.avatar','用户头像')->image('',80,80);
            $grid->column('user.nickname','用户昵称');
            $grid->column('profession.name','开店行业');
            $grid->disableCreateButton();
            $grid->column('exp_type')
                 ->display(function ($value){
                     return $value.'年';
                 });
            $grid->column('is_store')
                 ->using([1 => '是',2 => '否'])
                 ->label([1 => 'success',2 => 'danger']);
            $grid->column('plan_time');
            $grid->column('city');
            $grid->column('budget_amount');
            $grid->column('status')
                 ->using([1 => '待跟进',2 => '已跟进'])
                 ->label([1 => 'warning',2 => 'success']);


            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();

                $filter->like('user.name','用户昵称')
                    ->width(3);
                $filter->equal('firm_profession_id')
                    ->select(FirmProfession::getOption())
                    ->width(3);

                $filter->equal('status')
                      ->select([1 => '待跟进',2 => '已跟进'])
                     ->width(3);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserStorePlan(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('firm_profession_id');
            $show->field('exp_type');
            $show->field('is_store');
            $show->field('plan_time');
            $show->field('city');
            $show->field('budget_amount');
            $show->field('status');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserStorePlan(), function (Form $form) {
            $form->display('id');
            $form->selectTable('user_id')
                 ->from(UserTable::make())
                 ->model(User::class,'id','nickname')
                 ->required();
            $form->select('firm_profession_id')
                 ->options(FirmProfession::getOption())
                 ->required();
            $form->number('exp_type')
                 ->min(0)
                 ->required();
            $form->radio('is_store')
                 ->options([1 => '是',2 => '否']);
            $form->text('plan_time')
                 ->required();
            $form->text('city');
            $form->currency('budget_amount')
                 ->symbol('￥')
                 ->required();
            $form->radio('status')
                ->options([1 => '待跟进',2 => '已跟进'])
                ->required();
        });
    }
}
